package com.example.test;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.test.db.DBOpenHelper;
import com.example.test.db.User;

import java.util.List;

public class ModifyActivity extends AppCompatActivity {
    String phone,userName,email;
    EditText editText,editText1,editText2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify);
        editText=findViewById(R.id.userName1);
        editText1=findViewById(R.id.userPhone1);
        editText2=findViewById(R.id.userEmail1);
        String name=getIntent().getStringExtra("phone");

        DBOpenHelper db=new DBOpenHelper(ModifyActivity.this);
        List<User> list=db.query(name);
        for(User user:list){
            phone=user.getPhone();
            userName=user.getName();
            email=user.getEmail();
        }
        editText.setText(userName);
        editText1.setText(phone);
        editText2.setText(email);
    }

    public void fh(View view){
        ModifyActivity.this.finish();
    }

    public void xg(View view){
        String phone=getIntent().getStringExtra("phone");
        String xg1,xg2,xg3;
        editText=findViewById(R.id.userName1);
        editText1=findViewById(R.id.userPhone1);
        editText2=findViewById(R.id.userEmail1);
        xg1=editText.getText().toString();
        xg2=editText1.getText().toString();
        xg3=editText2.getText().toString();
        User user=new User();
        user.setName(xg1);
        user.setPhone(xg2);
        user.setEmail(xg3);
        DBOpenHelper db=new DBOpenHelper(ModifyActivity.this);
        int i=db.update(user);
        if (i>=0){
            Toast.makeText(ModifyActivity.this,"修改成功",Toast.LENGTH_SHORT).show();
            Intent intent1 = new Intent(ModifyActivity.this, HomeActivity.class);
            intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
            intent1.putExtra("name",xg1);
            intent1.putExtra("phone",phone);
            startActivity(intent1);

        }else{
            Toast.makeText(ModifyActivity.this,"修改失败",Toast.LENGTH_SHORT).show();
        }
    }
}